package hos.okgo.converter;

import java.io.IOException;
import java.lang.reflect.Type;

import hos.okhttp.utils.OkHttpResponseUtils;
import okhttp3.Response;

/**
 * <p>Title: JsonConvert </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-02-16 22:51
 */
public abstract class OkGoResponseConverter<T> extends OkGoConverter<T> {
    public OkGoResponseConverter() {
    }

    public OkGoResponseConverter(Type type) {
        super(type);
    }

    @Override
    public T convertResponse(Type succeed, Response response) throws IOException {
        try {
            return OkHttpResponseUtils.requireData(succeed, response);
        } catch (IOException e) {
            // 解析异常,重新解析
            return parseJson(succeed, OkHttpResponseUtils.requireResponse(response));
        }
    }

    /**
     * 反序列化JSON
     *
     * @param succeed JSON对象的类型
     * @receiver 原始字符串
     */
    public abstract T parseJson(Type succeed, Response response) throws IOException;
}
